<?php
/**
 * Created by 项目.
 * User: xuguopeng
 * Date: 2020-06-24
 * Time: 13:41
 */

namespace app\api\service\coupon;


use app\api\model\CouponUse;
use app\api\model\GoodsOrder;
use app\api\model\StoreOrder;
use app\api\model\User;
use app\api\service\Token;
use app\lib\exception\ParameterException;
use think\Db;
use think\Exception;

class CouponOrderSer extends Token
{
    /**
     * 在构造函数里面把需要的数据都添加好
     * 1.获取优惠券与订单信息
     * 2.判断优惠券
     * 3.判断订单
     * 4.修改优惠券类型为已绑定，也就是 3
     * 5.返回成功
     */
    // 类型
    private $type;

    // 优惠券信息
    private $couponUse;

    // 订单信息
    private $order;

    // 用户信息
    private $user;

    // 构造函数
    function __construct($type,$coupon_id,$order_id)
    {
        $this->type = $type;
        $this->user = User::get(self::getCurrentUid());
        $this->couponUse = CouponUse::with('coupon')->where('id',$coupon_id)->find();
        if($this->type == 1) {
            $this->order = StoreOrder::get($order_id);
        } elseif($this->type == 2){
            $this->order = GoodsOrder::get($order_id);
        }
    }

    // 入口方法
    public function entrance()
    {
        return $this->judgeCoupon();
    }

    // 判断优惠券
    private function judgeCoupon()
    {
        if(!$this->couponUse['coupon']) {
            throw new ParameterException([
                'msg' => [
                    'coupon_order' => '优惠券为空'
                ]
            ]);
        }
        if($this->couponUse['user_id'] != $this->user['id']) {
            throw new ParameterException([
                'msg' => [
                    'coupon_order' => '此优惠券不是您的优惠券'
                ]
            ]);
        }
        $time = time();
        if($this->couponUse['coupon']['start_time'] > $time) {
            throw new ParameterException([
                'msg' => [
                    'coupon_order' => '优惠券还未到使用时间'
                ]
            ]);
        }
        if($this->couponUse['coupon']['end_time'] < $time) {
            throw new ParameterException([
                'msg' => [
                    'coupon_order' => '优惠券已过期'
                ]
            ]);
        }
        if($this->couponUse['status'] == 3) {
            throw new ParameterException([
                'msg' => [
                    'coupon_order' => '此优惠券已经绑定订单'
                ]
            ]);
        }
        return $this->judgeOrder();
    }

    // 判断订单
    private function judgeOrder()
    {
        if(!$this->order) {
            throw new ParameterException([
                'msg' => [
                    'coupon_order' => '订单为空'
                ]
            ]);
        }
        if($this->order['user_id'] != $this->user['id']) {
            throw new ParameterException([
                'msg' => [
                    'coupon_order' => '此订单不是您的订单'
                ]
            ]);
        }
        if($this->couponUse['coupon']['money'] > $this->order['price']) {
            throw new ParameterException([
                'msg' => [
                    'coupon_order' => '此订单不满足次优惠券条件'
                ]
            ]);
        }
        if($this->type == 1) {
            if($this->order['status'] != 3) {
                throw new ParameterException([
                    'msg' => [
                        'coupon_order' => '此订单不是在支付状态'
                    ]
                ]);
            }
        } elseif($this->type == 1) {
            if($this->order['status'] != 1) {
                throw new ParameterException([
                    'msg' => [
                        'coupon_order' => '此订单不是在支付状态'
                    ]
                ]);
            }
        } else {
            throw new ParameterException([
                'msg' => [
                    'coupon_order' => '请传正确的类型值'
                ]
            ]);
        }
        return $this->updateCouponAndOrder();
    }

    // 更新优惠券与订单
    private function updateCouponAndOrder()
    {
        Db::startTrans();
        try
        {
            $couponUser = new CouponUse();
            $couponUser->save([ 'status' => 3 ],[ 'id' => $this->couponUse['id'] ]);
            if($this->type == 1) {
                $order = new StoreOrder();
                $order->save([
                    'coupon_use_id' => $this->couponUse['id'],
                    'coupon_price' => $this->couponUse['coupon']['price'],
                ], [ 'id' => $this->order['id'] ]);
            } else {
                $order = new GoodsOrder();
                $order->save([
                    'coupon_use_id' => $this->couponUse['id'],
                    'coupon_price' => $this->couponUse['coupon']['price'],
                ], [ 'id' => $this->order['id'] ]);
            }
            Db::commit();
            return writeJson(202, [], '绑定成功！');
        }
        catch (Exception $ex) {
            Db::rollback();
            throw $ex;
        }
    }
}